﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerApp
{
    public class ConcurrentInt
    {
        int index;

        Mutex wait = new Mutex();

        public ConcurrentInt() {
            index = 0;
        }

        public void GetAndAdd(ref int id) {
            lock (this) {
                wait.WaitOne();
                index++;
                id = index;
                wait.ReleaseMutex();
            }
        }

        public void Get(ref int id)
        {
            lock (this)
            {
                wait.WaitOne();
                id = index;
                wait.ReleaseMutex();
            }
        }

        public void Reset()
        {
            lock (this)
            {
                wait.WaitOne();
                index=0;
                wait.ReleaseMutex();
            }
        }
    }
}
